﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyPartnerAddControl : YuanlinUserControl
    {
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedDropDownList categoryDropDownList;
        protected ValuedTextBox linkUrlValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            CompanyPartner companyPartner = new CompanyPartner();

            companyPartner.Title.Value = CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
            companyPartner.LinkUrl.Value = CensorWordManager.CensorWord(this.linkUrlValuedTextBox.Value);
            companyPartner.CategoryId.Value = int.Parse(categoryDropDownList.Value);
            companyPartner.UserId.Value = user.EntityId.Value;

            if (BusinessManager.CreateCompanyPartner(companyPartner) == -2)
            {
                Globals.ShowMessage(this.Page, "添加失败", "此信息已经存在！");
                return;
            }

            if (string.IsNullOrEmpty(user.CompanyPartner))
            {
                user.CompanyPartner = "1";
            }
            else
            {
                user.CompanyPartner = (Int32.Parse(user.CompanyPartner) + 1).ToString();
            }
            UserManager.UpdateUser(user);

            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyPartnerListUrl());
        }
        private void Bind()
        {
            CompanyPartnerCategoryListRequest request = new CompanyPartnerCategoryListRequest();
            request.UserId = user.EntityId.Value;
            request.PageIndex = 0;
            request.PageSize = 1000;
            EntityList categories = BusinessManager.GetCompanyPartnerCategoryList(RequestHelper.BuildGetCompanyPartnerCategoryListRequest(request));
            categoryDropDownList.DataSource = categories;
            categoryDropDownList.DataValueField = "EntityId";
            categoryDropDownList.DataTextField = "Title";
            categoryDropDownList.DataBind();
        }
    }
}